import axios from "axios";

// 普通请求
const request = axios.create({
  baseURL: "/dev",
  timeout: 5000,
});

// 普通请求拦截器
request.interceptors.request.use(
  (config) => {
    config.headers["Content-Language"] = "zh_CN";
    const isToken = (config.headers || {}).isToken === false;
    // 设置token
    if (!isToken && localStorage.getItem("token")) {
      config.headers["Authorization"] = "Bearer " + localStorage.getItem("token"); // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    return config;
  },
  (err) => {}
);

// 普通响应拦截器
request.interceptors.response.use(
  (res) => {
    return res.data;
  },
  (err) => {
    return new Promise.reject(new Error(err));
  }
);

// ai聊天
const aiRequest = axios.create({
  baseURL: "https://api.sizhi.com",
  timeout: 5000,
});

// ai的请求拦截器
aiRequest.interceptors.request.use(
  (config) => {
    config.headers["Content-Language"] = "zh_CN";
    console.log(config);
    return config;
  },
  (err) => {}
);

// ai的响应拦截器
aiRequest.interceptors.response.use(
  (res) => {
    console.log(res);
    return res.data.data;
  },
  (err) => {
    return new Promise.reject(new Error(err));
  }
);

export default request;

export { aiRequest };
